package com.payment.common.utils;

import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Properties;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Created by fuzhili
 */
public class PropertiesUtil {
    private static final Logger log = LoggerFactory.getLogger(PropertiesUtil.class);

    private static Properties props;

    static {
        String fileName = "default.properties";
        props = new Properties();
        try {
            props.load(new InputStreamReader(PropertiesUtil.class.getClassLoader().getResourceAsStream(fileName),"UTF-8"));

        } catch (IOException e) {
            log.error("配置文件读取异常",e);

        }
    }

    public static String getProperty(String key){
        String value = props.getProperty(key.trim());
        if(StringUtils.isBlank(value)){
            return null;
        }
        return value.trim();
    }

    @SuppressWarnings("unchecked")
	public static <T> T getProperty(String key,Class<T> clazz){
        String value=getProperty(key);
        if(Integer.class.equals(clazz)){
            return (T)Integer.valueOf(value);
        }else if(Byte.class.equals(clazz)){
            return (T)Byte.valueOf(value);
        }else if(Long.class.equals(clazz)){
            return (T)Long.valueOf(value);
        }
        return (T)value;
    }

    public static String getProperty(String key,String defaultValue){

        String value = props.getProperty(key.trim());
        if(StringUtils.isBlank(value)){
            value = defaultValue;
        }
        return value.trim();
    }



}
